package com.waijiaojun.tpo.web;

import com.waijiaojun.tpo.entity.GoodsOrder;
import com.waijiaojun.tpo.rest.RestClientException;
import com.waijiaojun.tpo.rest.RestResponse;
import com.waijiaojun.tpo.rest.RestServerException;
import com.waijiaojun.tpo.service.GoodsOrderService;
import com.waijiaojun.tpo.service.weixin.WeixinPayService;
import com.waijiaojun.tpo.service.weixin.WeixinUserInfoService;
import com.waijiaojun.tpo.utils.Constants;
import com.waijiaojun.tpo.utils.IPUtil;
import com.waijiaojun.tpo.utils.JsonUtil;
import com.waijiaojun.tpo.utils.UserUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: xvitcoder
 * Date: 12/20/12
 * Time: 5:27 PM
 */
@Controller
@RequestMapping("/payment")
public class PaymentController {
    private static int flag = 0;
    private static Logger logger = LoggerFactory.getLogger(PaymentController.class);

    @Autowired
    private GoodsOrderService goodsOrderService;
    @Autowired
    private WeixinUserInfoService weixinUserInfoService;

    @RequestMapping(value = "order/create")
    @ResponseBody
    public String createOrder() {
        return "index";
    }

    @RequestMapping("isPay")
    @ResponseBody
    public RestResponse isPay() {
        //打印入参
        logger.info(Constants.prefix_in );

        RestResponse restResponse = RestResponse.getSuccessResponse();
        try {
            String openid=UserUtil.getCurrentUserOpenId();
            int isPay = goodsOrderService.isPay(openid);
            int isBindPhone = weixinUserInfoService.isBindPhone(openid);
            Map<String, Integer> data = new HashMap<>();
            data.put("isPay", isPay);// 0:未购买，1:已购买
            data.put("isBindPhone", isBindPhone);// 0:未绑定手机号，1:已绑定手机号
            restResponse.setData(data);
        } catch (RestClientException e) {
            logger.error(e.getMessage(), e);
            return RestResponse.getClientErrorResponse(e.getMessage());
        } catch (RestServerException e) {
            logger.error(e.getMessage(), e);
            return RestResponse.getServerErrorResponse(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return RestResponse.getServerErrorResponse(e.getMessage());
        }
        //打印出参
        logger.info(Constants.prefix_out + JsonUtil.toJson(restResponse));
        return restResponse;
    }

    // 是否扫码
    @RequestMapping("isScanQrCode")
    @ResponseBody
    public RestResponse isScanQrCode() {
        //打印入参
        logger.info(Constants.prefix_in );

        RestResponse restResponse = RestResponse.getSuccessResponse();
        try {
            String openid=UserUtil.getCurrentUserOpenId();
            int isScanQrCode = goodsOrderService.isScanQrCode(openid);// 是否扫码：0否，1是
            restResponse.setData(isScanQrCode);
        } catch (RestClientException e) {
            logger.error(e.getMessage(), e);
            return RestResponse.getClientErrorResponse(e.getMessage());
        } catch (RestServerException e) {
            logger.error(e.getMessage(), e);
            return RestResponse.getServerErrorResponse(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return RestResponse.getServerErrorResponse(e.getMessage());
        }
        //打印出参
        logger.info(Constants.prefix_out + JsonUtil.toJson(restResponse));
        return restResponse;
    }

    /**
     * 先创建订单，然后调用支付接口。这样做有两个原因： 1. 订单创建失败: 提交订单，并支付(有支付成功和失败两种状态)。 2. 订单创建成功:
     * 弹出订单详情页面，并显示订单的状态。
     *
     * @return
     */
    @RequestMapping(value = "/createOrder", method = RequestMethod.POST)
    @ResponseBody
    public RestResponse createOrder(HttpServletRequest request) {
        //打印入参
        RestResponse restResponse = RestResponse.getSuccessResponse();
        Map<String, String> params = null;
        try {
            String openid=UserUtil.getCurrentUserOpenId();
            GoodsOrder goodsOrder = new GoodsOrder();
            // 创建订单
            goodsOrder.setOpenid(openid);
            goodsOrder.setIp(IPUtil.getIp(request));
            params = goodsOrderService.createOrder(goodsOrder);

            restResponse.setData(params);
        } catch (RestClientException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getClientErrorResponse(e.getMessage());
        } catch (RestServerException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        }

        //打印出参
        logger.info(Constants.prefix_out + JsonUtil.toJson(restResponse));
        return restResponse;
    }



}
